package com.hippo4j;

import com.gj.executor.support.ResizableCapacityLinkedBlockingQueue;
import com.hippo4j.executor.DynamicThreadPoolExecutor;
import com.hippo4j.executor.ExtensibleThreadPoolExecutor;
import com.hippo4j.executor.support.ThreadPoolBuilder;
import com.hippo4j.plugin.impl.TaskRejectCountRecordPlugin;
import com.hippo4j.plugin.impl.ThreadPoolExecutorShutDownPlugin;
import com.hippo4j.plugin.manager.DefaultThreadPoolPluginManager;
import com.hippo4j.plugin.manager.ThreadPoolPluginManager;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {

    public static void main(String[] args) {
//        System.out.println("hello world");
//        testResizableLinkedBlockingQueue();
//        testPlugin();
//        extracted();

//        testDynamicThreadPool();
        testThreadPoolBuilder();
    }

    private static void testThreadPoolBuilder() {
        ThreadPoolExecutor executor = ThreadPoolBuilder
                .builder()
                .isDynamic(true)
                .threadPoolId("1")
                .corePoolSize(10)
                .maximumPoolSize(100)
                .keepAliveTime(10)
                .queueCapacity(100)
                .build();

        executor.execute(() -> {
            System.out.println(Thread.currentThread() + ": hello world");
        });
        executor.execute(() -> {
            System.out.println(Thread.currentThread() + ": hello world");
        });
        executor.execute(() -> {
            System.out.println(Thread.currentThread() + ": hello world");
        });
    }

    private static void testDynamicThreadPool() {
        DynamicThreadPoolExecutor executor = new DynamicThreadPoolExecutor(
                "1",
                1,
                1,
                5,
                TimeUnit.SECONDS,
                new ResizableCapacityLinkedBlockingQueue<>(1),
                r -> new Thread(r),
                new ThreadPoolExecutor.AbortPolicy(),
                false
        );

//        for (int i = 0; i < 50; i++) {
//            executor.execute(() -> {
//                try {
//                    Thread.sleep(10000);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//                System.out.println("hello world");
//            });
//        }

        executor.execute(() -> System.out.println("hello world"));
        executor.execute(() -> System.out.println("hello world"));
        executor.execute(() -> System.out.println("hello world"));

    }

    private static void extracted() {
        Thread thread = new Thread(() -> {
            while (true) {
                System.out.println("666");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        thread.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        thread = null;


        System.out.println();
    }


    public static void testPlugin() {

        ThreadPoolPluginManager pluginManager = new DefaultThreadPoolPluginManager();
//        pluginManager.registerPlugin(new TaskTimeOutNotifyAlarmPlugin(500));
        pluginManager.registerPlugin(new ThreadPoolExecutorShutDownPlugin());
        pluginManager.registerPlugin(new TaskRejectCountRecordPlugin());

        ExtensibleThreadPoolExecutor threadPoolExecutor = new ExtensibleThreadPoolExecutor("1",
                1,
                1,
                1,
                TimeUnit.SECONDS,
                new ResizableCapacityLinkedBlockingQueue<>(1),
                r -> new Thread(r),
                new ThreadPoolExecutor.AbortPolicy(),
                pluginManager);


        threadPoolExecutor.execute(() -> {
            try {
                TimeUnit.SECONDS.sleep(100000000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        threadPoolExecutor.execute(() -> {
            try {
                TimeUnit.SECONDS.sleep((long) 0.1);
                System.out.println("hello world");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        threadPoolExecutor.execute(() -> {
            try {
                TimeUnit.SECONDS.sleep((long) 0.1);
                System.out.println("hello world");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        threadPoolExecutor.execute(() -> {
            try {
                TimeUnit.SECONDS.sleep((long) 0.1);
                System.out.println("hello world");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

//        threadPoolExecutor.shutdown();
    }

    public static void testResizableLinkedBlockingQueue() {
        ResizableCapacityLinkedBlockingQueue<Integer> queue = new ResizableCapacityLinkedBlockingQueue<>(3);
        for (int i = 0; i < 6; i++) {
            boolean offered = queue.offer(i);
            System.out.println("offered = " + offered + " 要放入的元素: " + i);
        }

        queue.clear();

        queue.setCapacity(6);

        for (int i = 0; i < 6; i++) {
            boolean offered = queue.offer(i);
            System.out.println("offered = " + offered + " 要放入的元素: " + i);
        }
    }
}
